package xddq

import (
	"fmt"
	"strings"
	D "websocket_apn/define"
	"websocket_apn/log"
	"websocket_apn/proto"
)

/*
灵兽召唤-召唤灵兽（00 左边 01-中间）
[DEBUG] [2024/04/25 00:45:29] [PRJ/websocket_apn/main_test.go]:[  194]:   S_PET_CATCH - id:3109673861602
[DEBUG] [2024/04/25 00:45:29] [PRJ/websocket_apn/main_test.go]:[  195]:   PayloadBytes: 71 AB 00 00 00 14 00 00 51 05 00 00 02D4 07 01 81 E2 08 01
灵兽召唤-第二个升级
[DEBUG] [2024/04/25 00:51:01] [PRJ/websocket_apn/main_test.go]:[  194]:   S_PET_LEVEL_UP - id:3109673861602
[DEBUG] [2024/04/25 00:51:01] [PRJ/websocket_apn/main_test.go]:[  195]:   PayloadBytes: 71 AB 00 00 00 14 00 00 51 09 00 00 02D4 07 01 81 E2 08 2E
灵兽召唤-第四个天马升级
[DEBUG] [2024/04/25 00:52:08] [PRJ/websocket_apn/main_test.go]:[  194]:   S_PET_LEVEL_UP - id:3109673861602
[DEBUG] [2024/04/25 00:52:08] [PRJ/websocket_apn/main_test.go]:[  195]:   PayloadBytes: 71 AB 00 00 00 14 00 00 51 09 00 00 02D4 07 01 81 E2 08 31

登录返回的灵兽信息
[DEBUG] [2024/05/05 10:59:17] [PRJ/websocket_apn/main_test.go]:[  196]:   game cmd[740]-unkown - id:3109700251759
[DEBUG] [2024/05/05 10:59:17] [PRJ/websocket_apn/main_test.go]:[  197]:   PayloadBytes: 71 AB 00 00 01 56 00 00 02 E4 00 00 02D4 08 94 30 6F 0A C1 02 0A 06 08 81 EB 06 10 00 0A 06 08 9C E3 06 10 00 0A 06 08 99 E3 06 10 00 10 06 1A 2D 08 01 10 F0 F2 06 18 05 20 00 2A 06 08 B6 BF 05 10 01 2A 06 08 9D BF 05 10 01 30 00 50 00 60 00 68 00 70 00 80 01 01 90 01 00 98 0100 1A 25 08 02 10 99 E3 06 18 01 20 00 2A 06 08 CD BF 05 10 01 30 00 50 00 60 00 68 00 70 00 80 01 01 90 01 00 98 01 00 1A 25 08 03 10 85 EB 06 18 01 20 00 2A 06 08 B6 BF 05 10 01 30 00 50 00 60 00 68 00 70 00 80 01 01 90 01 00 98 01 00 1A25 08 04 10 99 E3 06 18 01 20 00 2A 06 08 A1 BF 05 10 01 30 00 50 00 60 00 68 00 70 00 80 01 01 90 01 00 98 01 00 1A 2D 08 05 10 ED F2 06 18 01 20 00 2A 06 08 B1 BF 05 10 01 2A 06 08 9E BF 05 10 01 30 00 50 00 60 00 68 00 70 00 80 01 01 9001 00 98 01 00 1A 25 08 06 10 9D E3 06 18 01 20 00 2A 06 08 CC BF 05 10 01 30 00 50 00 60 00 68 00 70 00 80 01 01 90 01 00 98 01 00 20 01 28 00 30 F0 F2 06 30 99 E3 06 30 85 EB 06 30 ED F2 06 30 9D E3 06 38 00 40 00 48 00 50 00 58 0078 FF FF FF FF FF FF FF FF FF 01
*/
func CheckPetInfoIfNeedBy(info D.AllPetInfo) (idx int, needBy bool) {
	idx = -1
	needBy = false //是否启动自动购买
	for num, data := range info.PetInfo {
		if data.HasBuy {
			continue
		}
		if strings.Contains(data.Name, "0x") {
			idx = num
			log.Infof("灵兽-遇到未知灵兽【%s】", data.Name)
			return
		} else if strings.Contains(data.Name, "玄武") || strings.Contains(data.Name, "青龙") {
			idx = num
			log.Infof("灵兽-当前已刷出【%s】", data.Name)
			needBy = true
			return
		} else if strings.Contains(data.Name, "应龙") { //|| strings.Contains(data.Name, "鸾鸟") {
			idx = num
			log.Infof("灵兽-当前已刷出【%s】, 准备自动购买", data.Name)
			needBy = true
			return
		}
	}
	return
}

// 检查宠物信息
func (xd *XdDriver) CheckAllPetInfoData(info D.AllPetInfo) {
	if len(info.PackPet) > 0 {
		xd.UserData.PackPet = info.PackPet
	}
	idx, needBy := CheckPetInfoIfNeedBy(info)
	if info.PetInfo[0].ID[0] == 0 && info.PetInfo[1].ID[0] == 0 && info.PetInfo[2].ID[0] == 0 {
		return
	}
	if info.IsByLogin {
		if idx < 0 { // && xd.UserData.PetCall > 0 && !needBy
			xd.ResetTaskByID(proto.S_PET_REFRESH_POOL)
		}
	} else {
		if idx >= 0 {
			xd.StopTaskByID(proto.S_PET_REFRESH_POOL)
		}
	}
	if xd.UserData.PetCall > 0 {
		for i := 0; i < 3 && xd.UserData.PetCall > 0; i++ {
			if !info.PetInfo[i].HasBuy && info.PetInfo[i].ID[1] == 0xE3 {
				log.Infof("灵兽召唤-开始消耗御灵石召唤【%s】", info.PetInfo[i].Name)
				xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_PET_CATCH, fmt.Sprintf("08%02X", i))) //召唤灵兽
				xd.UserData.PetCall--
			}
		}
	}
	if needBy && idx >= 0 {
		log.Infof("灵兽召唤-开始消耗御灵石召唤【%s】", info.PetInfo[idx].Name)
		xd.Send(proto.Get_Cmd_Data(xd.userInfo.PlayerId, proto.S_PET_CATCH, fmt.Sprintf("08%02X", idx))) //召唤灵兽
	}

}
